import 'package:boss/color_constant.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/analysis/provider/analysis_provider.dart';
import 'package:boss/ui/widget/image/base_image.dart';
import 'package:boss/ui/widget/shadow_radius_card.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../tooltip_icon.dart';

class RevenueDataCard extends StatelessWidget {
  final String title;
  final String titleTip;
  final int centerText;
  final String yesterdayPercentage;
  final int storeDataText;
  final String storePercentage;
  final int takeoutDataText;
  final String takeoutPercentage;
  final String unit;
  final int type;

  const RevenueDataCard({
    Key key,
    this.title,
    this.titleTip,
    this.centerText,
    this.yesterdayPercentage,
    this.storeDataText,
    this.storePercentage,
    this.takeoutDataText,
    this.takeoutPercentage,
    this.unit,
    this.type,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return DefaultTextStyle(
      style: TextStyleConstant().normal_1_28,
      child: ShadowRadiusCard(
        margin: EdgeInsets.only(bottom: 30.w),
        padding: EdgeInsets.all(20.w),
        child: Row(
          children: <Widget>[
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      Text(title),
                      Container(
                        child: ToolTipIcon(titleTip),
                        margin: EdgeInsets.only(left: 10.w),
                      ),
                    ],
                  ),
                  Container(
                      margin: EdgeInsets.only(
                        top: 20.w,
                        left: 20.w,
                      ),
                      child: type == 0 ? _PriceText(centerText, 0) : _OrderText(centerText, unit, 0)),
                  Container(
                    margin: EdgeInsets.only(
                      top: 10.w,
                      left: 20.w,
                    ),
                    child: Row(
                      children: <Widget>[
                        Selector<AnalysisProvider, String>(
                          selector: (context, provider) => provider.revenueDateText,
                          builder: (context, data, _) => Text(
                            data,
                            style: TextStyle(
                              fontSize: 28.sp,
                              color: ColorConstant.ffcc,
                            ),
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.only(
                            left: 20.w,
                          ),
                          child: Text(yesterdayPercentage + '%'),
                        )
                      ],
                    ),
                  ),
                ],
              ),
            ),
            Container(
              width: 240.w,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  _IconMessage(
                    icon: 'assets/image/store_text_icon.png',
                    top: storeDataText,
                    bottom: storePercentage,
                    type: type,
                    unit: unit,
                  ),
                  if (takeoutDataText != null || takeoutPercentage != null)
                    SizedBox(
                      height: 20.w,
                    ),
                  if (takeoutDataText != null || takeoutPercentage != null)
                    _IconMessage(
                      icon: 'assets/image/takeout_text_icon.png',
                      top: takeoutDataText,
                      bottom: takeoutPercentage,
                      type: type,
                      unit: unit,
                    ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class RevenueCouponDataCard extends StatelessWidget {
  final int sendCouponCount;
  final int usedCouponCount;
  final int usedCouponCountStoreDineCount;
  final int usedCouponCountTakeOutCount;
  final String usedCouponCountStoreDinePercent;
  final String usedCouponCountTakeOutPercent;

  const RevenueCouponDataCard({
    this.sendCouponCount,
    this.usedCouponCount,
    this.usedCouponCountStoreDineCount,
    this.usedCouponCountTakeOutCount,
    this.usedCouponCountStoreDinePercent,
    this.usedCouponCountTakeOutPercent,
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return DefaultTextStyle(
      style: TextStyleConstant().normal_1_28,
      child: ShadowRadiusCard(
        margin: EdgeInsets.only(bottom: 30.w),
        padding: EdgeInsets.only(top: 20.w, bottom: 20.w, left: 40.w, right: 20.w),
        child: Row(
          children: <Widget>[
            Expanded(
              child: Row(
                children: [
                  buildData('已派券', sendCouponCount),
                  buildData('核销券', usedCouponCount),
                ],
              ),
            ),
            Container(
              width: 240.w,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  _IconMessage(
                    icon: 'assets/image/store_text_icon.png',
                    top: usedCouponCountStoreDineCount,
                    bottom: usedCouponCountStoreDinePercent,
                    type: 1,
                    unit: '单',
                  ),
                  SizedBox(
                    height: 20.w,
                  ),
                  _IconMessage(
                    icon: 'assets/image/takeout_text_icon.png',
                    top: usedCouponCountTakeOutCount,
                    bottom: usedCouponCountTakeOutPercent,
                    type: 1,
                    unit: '单',
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget buildData(String title, int value) {
    return Expanded(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(title),
          RichText(
            text: TextSpan(
              text: '$value',
              style: TextStyleConstant().normal_1_50.copyWith(
                fontFamily: 'DINAlternate-Bold'
              ),
              children: [
                TextSpan(
                  text: '张',
                  style: TextStyleConstant().normal_2_28,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class _IconMessage extends StatelessWidget {
  final String icon;
  final int top;
  final String bottom;
  final int type;
  final String unit;

  const _IconMessage({
    this.icon,
    this.top,
    this.bottom,
    this.type,
    this.unit,
  });

  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        BaseImage(
          icon,
          width: 74.w,
          height: 74.w,
        ),
        Expanded(
          child: Container(
            height: 74.w,
            margin: EdgeInsets.only(
              left: 20.w,
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                if (top != null) type == 0 ? _PriceText(top, 1) : _OrderText(top, unit, type),
                Text(
                  '${bottom == null || bottom == '' ? 0 : bottom}%',
                  style: TextStyleConstant().normal_2_24,
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }
}

class _PriceText extends StatelessWidget {
  final int value;
  final int type;

  const _PriceText(this.value, this.type);

  @override
  Widget build(BuildContext context) {
    final unitStyle = type == 0
        ? TextStyle(fontSize: 28.sp, color: ColorConstant.ff66, fontFamily: 'DINAlternate-Bold')
        : TextStyle(fontSize: 20.sp, color: ColorConstant.ff66, fontFamily: 'DINAlternate-Bold');
    final yuanStyle = type == 0
        ? TextStyle(fontSize: 50.sp, color: ColorConstant.ff33, fontFamily: 'DINAlternate-Bold')
        : TextStyle(fontSize: 28.sp, color: ColorConstant.ff33, fontFamily: 'DINAlternate-Bold');
    final fenStyle = type == 0
        ? TextStyle(fontSize: 28.sp, color: ColorConstant.ff33, fontFamily: 'DINAlternate-Bold')
        : TextStyle(fontSize: 20.sp, color: ColorConstant.ff33, fontFamily: 'DINAlternate-Bold');

    final yuan = value ~/ 100;
    var fen = value % 100;
    var fenText = fen.toString();
    if (fenText.length == 1) {
      fenText = '${fenText}0';
    }

    return RichText(
      text: TextSpan(
        text: '¥',
        style: unitStyle,
        children: [
          TextSpan(
            text: '$yuan',
            style: yuanStyle,
          ),
          TextSpan(
            text: '.$fenText',
            style: fenStyle,
          ),
        ],
      ),
    );
  }
}

class _OrderText extends StatelessWidget {
  final int value;
  final int type;
  final String unit;

  const _OrderText(this.value, this.unit, this.type);

  @override
  Widget build(BuildContext context) {
    final unitStyle = type == 0
        ? TextStyle(fontSize: 28.sp, color: ColorConstant.ff66, fontFamily: 'DINAlternate-Bold')
        : TextStyle(fontSize: 20.sp, color: ColorConstant.ff66, fontFamily: 'DINAlternate-Bold');
    final valueStyle = type == 0
        ? TextStyle(fontSize: 50.sp, color: ColorConstant.ff33, fontFamily: 'DINAlternate-Bold')
        : TextStyle(fontSize: 28.sp, color: ColorConstant.ff33, fontFamily: 'DINAlternate-Bold');

    return RichText(
      text: TextSpan(
        text: '$value',
        style: valueStyle,
        children: [
          TextSpan(
            text: unit,
            style: unitStyle,
          ),
        ],
      ),
    );
  }
}
